﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Search : System.Web.UI.Page
{
    public static string SearchType=String.Empty;
    string keyword = string.Empty;
    int ItemsPerPage = 3;
    int PageSize = 0;
    int currentpage = 1;
    security baomat = new security();
    private int RowCount
    {
        get
        {
            return (int)ViewState["RowCount"];
        }
        set
        {
            ViewState["RowCount"] = value;
        }
    }
    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Tìm kiếm";
        try
        {
            if (Page.RouteData.Values.Count > 0)
            {
                keyword = Page.RouteData.GetRequiredString("keyword");
                SearchType = Page.RouteData.GetRequiredString("category");
                
                if (!IsPostBack)
                {
                    GetSearchData(ItemsPerPage, PageSize, SearchType);
                }
                else
                {
                    plcPaging.Controls.Clear();
                    CreatePagingControl();
                }
                //    var searchUser = from u in dc.Users
                //                     where u.Users_Address.Contains(keyword) || u.Users_Name.Contains(keyword) || u.Users_SkypeNick.Contains(keyword) || u.Users_YahooNick.Contains(keyword) || u.Users_Email.Contains(keyword) || u.Users_FullName.Contains(keyword) || u.Users_MobilePhone.Contains(keyword) || u.Users_HomePhone.Contains(keyword)
                //                     select new { u.Users_Id, u.Users_Name, Type = "Users" };

            }
        }
        catch (Exception)
        {
        }
    }
    #endregion
    private void GetSearchData(int take, int pagesize,string type)
    {
        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                switch (type.ToLower())
                {
                    case "dn":
                        var result = (from o in dc.EStores
                                     join u in dc.Users on o.Users_Id equals u.Users_Id
                                     where o.EStore_Address.Contains(keyword) || o.EStore_Name.Contains(keyword) || o.EStore_Content.Contains(keyword) && o.EStore_StatusId==2
                                     orderby o.EStore_LuckCyclePoint descending
                                     orderby o.EStore_View descending                                     
                                     select new { o.EStore_Id, o.EStore_Name,o.EStore_MobilePhone, o.Estore_HomePhone,u.Users_Email, o.EStore_Logo })
                                     .Take(take)
                                     .Skip(pagesize);
                        if (result.Count() > 0)
                        {
                            lblNorecord.Visible = false;
                            PagedDataSource page = new PagedDataSource();
                            page.AllowCustomPaging = true;
                            page.AllowPaging = true;
                            page.DataSource = result.ToList();
                            page.PageSize = take;
                            rptSearch.DataSource = page;
                            rptSearch.DataBind();

                            if (!IsPostBack)
                            {
                                RowCount = result.Count();
                                CreatePagingControl();
                            }
                            removeClass();
                            string OptionID = "lnkPage" + currentpage;
                            LinkButton lnk = new LinkButton();
                            lnk = (LinkButton)plcPaging.FindControl(OptionID);
                            lnk.CssClass = "number_selected";
                            lnk.Enabled = false;
                        }
                        else
                            lblNorecord.Visible = true;
                        break;
                    case "pr":
                        break;
                }
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    public string GetCompanyName(string CompanyName)
    {
        CompanyName = baomat.RemoveSpecialCharacter(CompanyName).Replace(" ","-").ToLower();
        return CompanyName;
    }
    private void CreatePagingControl()
    {
        for (int i = 0; i < (RowCount / ItemsPerPage) + 1; i++)
        {
            LinkButton lnk = new LinkButton();
            lnk.Click += new EventHandler(lbl_Click);
            lnk.ID = "lnkPage" + (i + 1).ToString();
            lnk.Text = (i + 1).ToString();
            lnk.CssClass = "number";
            plcPaging.Controls.Add(lnk);
            currentpage = i+1;
        }
    }
    private void removeClass()
    {
        for (int i = 0; i < (RowCount / ItemsPerPage) + 1; i++)
        {
            string OptionID = "lnkPage" + (i+1);
            LinkButton lnk = new LinkButton();
            lnk = (LinkButton)plcPaging.FindControl(OptionID);
            lnk.CssClass = "number";
            lnk.Enabled = true;
        }
    }
    void lbl_Click(object sender, EventArgs e)
    {
        LinkButton lnk = sender as LinkButton;
        int currentPage = int.Parse(lnk.Text);
        int take = currentPage * ItemsPerPage;
        int skip = currentPage == 1 ? 0 : take - ItemsPerPage;
        GetSearchData(take, skip, SearchType);
    }
}